package com.xwh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwh.pojo.entity.Student;
import com.xwh.pojo.vo.StudentVo;
import com.xwh.service.IStudentService;
import com.xwh.util.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 向文豪
 * @program: springboot0412
 * @description:
 * @date 2023/4/12 19:58
 */
@RestController
@RequestMapping("student")
@Api(tags = "学生操作API")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @PostMapping("get/{current}/{pageSize}")
    @ApiOperation(value = "根据条件查询学生信息")
    public Result getByPage(
            @ApiParam(value = "当前页", name = "current", required = false, defaultValue = "1")
            @PathVariable Integer current,
            @ApiParam(value = "每页条数", name = "pageSize", required = false, defaultValue = "5")
            @PathVariable Integer pageSize,
            @RequestBody StudentVo studentVo
    ) {
        return studentService.findPage(current, pageSize, studentVo);
    }


    @GetMapping("findAll")
    @ApiOperation(value = "查询学生信息")
    public Result findAll(){
       return studentService.findAll();

    }

    @GetMapping("findById/{sid}")
    @ApiOperation(value = "根据id查询学生信息")
    public Result findById(@ApiParam(value = "学生id")
                           @PathVariable Integer sid){
        return  studentService.findById(sid);
    }

    @PostMapping("insert")
    @ApiOperation(value = "添加学生信息")
    public Result insert(@RequestBody Student student){
        return studentService.insert(student);
    }


    @PutMapping("update")
    @ApiOperation(value = "修改学生信息")
    public Result update(@RequestBody Student student){
        return studentService.updateById(student);
    }

    @DeleteMapping("delete/{sid}")
    @ApiOperation(value = "根据id删除学生信息")
    public Result delete(@ApiParam(value = "学生id") @PathVariable  Integer sid){
       return  studentService.deleteById(sid);
    }

    @DeleteMapping("deleteIds/{ids}")
    @ApiOperation(value = "删除多条学生信息")
    public Result deleteIds(@ApiParam(value = "学生id")
                            @PathVariable List<Integer> ids){
        return studentService.deleteByIds(ids);
    }

    @GetMapping("getPage/{current}/{pageSize}")
    @ApiOperation(value = "单表分页查询")
    public Result getPage(
            @ApiParam(value = "当前页", name = "current", required = false, defaultValue = "1")
            @PathVariable Integer current,
            @ApiParam(value = "每页条数", name = "pageSize", required = false, defaultValue = "5")
            @PathVariable Integer pageSize
    ){
        Page<Student> page = new Page<>(current,pageSize);
        return studentService.getPage(page,null);
    }
}
